
var  client = require('../database');
mysql = client.getDbCon();

//获取应用列表数据
exports.getAllApp = function(req,res){
    var selectNameSQL = "select * from tb_applist ";
    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            return res.render('applist',{appdata:data});
        }
    });
};


//增加一条App列表数据
exports.createAppInfo = function(req,res){
    var appname = req.body.appname;
    var version = req.body.version;
    var status = req.body.status;

    var insertSQL = 'insert into tb_applist values(null,' +'"'+ appname +'"' +',' + '"'+version+'"' +',' + '"'+status+'"' +')';

    mysql.query(insertSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAllApp');
        }
    });
};

exports.editAppInfo = function(req,res){
    var appname = req.body.appname;
    var id = req.body.id;
    var status = req.body.status;
    var version = req.body.version;

    var updateSQL = 'update tb_applist set app_name=' +'"'+ appname +'"' +',' +'version='+ '"'+version+'"' +',' +'status='+ '"'+status+'"'+'where id='+'"'+ id +'"';
    mysql.query(updateSQL,function(err,rows){
        if (err) console.log(err);
        console.log("UPDATE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAllApp');
        }
    });
};

exports.deleteApp = function(req,res){
    var id = req.query['id'];
    var deleteSQL = 'delete from tb_applist  where id=' +'"'+ id +'"' ;
    mysql.query(deleteSQL,function(err,rows){
        if (err) console.log(err);
        console.log("DELETE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAllApp');
        }
    });
};